package service

import (
	"errors"
	"fanqiechaodan-blog/dao"
	"fanqiechaodan-blog/models"
	"fanqiechaodan-blog/utils"
	"go.uber.org/zap"
)

func Login(userName, passwd string) (*models.LoginRes, error) {
	passwd = utils.Md5Crypt(passwd)
	user := dao.GetUser(userName, passwd)
	if user == nil {
		return nil, errors.New("账号/密码错误")
	}
	uid := user.Uid
	token, err := utils.Award(&uid)
	if err != nil {
		zap.S().Errorf("uid:%dtoken生成失败;原因:%s", uid, err.Error())
		return nil, errors.New("token生成失败")
	}
	return &models.LoginRes{
		Token: token,
		UserInfo: models.UserInfo{
			Uid:      uid,
			UserName: userName,
			Avatar:   user.Avatar,
		},
	}, nil
}
